package com.yanqu.road.server.logic.activity.orewar.condition;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UserActivityConditionData;
import com.yanqu.road.entity.task.args.UnionRiseRankArgs;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.orewar.OreWarActivityModule;
import com.yanqu.road.server.logic.activity.orewar.OreWarActivityData;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.orewar.OreWarActivityMgr;
import com.yanqu.road.server.manger.activity.orewar.OreWarActivityRankMgr;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;

public abstract class BaseOreWarActivityUnionRankCondition extends BaseOreWarActivityCondition{

    public BaseOreWarActivityUnionRankCondition(GamePlayer player, OreWarActivityData activityData, ActivityConditionInfo conditionInfo) {
        super(player, activityData, conditionInfo);
    }

    public final void setUnionRankActivityDefaultValue(String unionUid, int notifyType) {
        if(!StringUtils.isNullOrEmpty(unionUid)) {
            UserActivityConditionData userActivityConditionData = activityData.getUserActivityConditionData(getConditionId());
            setUnionActivityData(unionUid, notifyType, userActivityConditionData);
        }
    }

    public final void setUnionRankActivityValue(BigInteger value, String unionUid, int notifyType) {
        UserActivityConditionData userActivityConditionData = activityData.getUserActivityConditionData(getConditionId());
        userActivityConditionData.setValue(value);
        setUnionActivityData(unionUid, notifyType, userActivityConditionData);
    }

    private void setUnionActivityData(String unionUid, int notifyType, UserActivityConditionData userActivityConditionData) {
        //活动到了结束但是还没领奖的时间可以继续触发系统的撤回，但是不触发商会操作了
        ActivityInfo activityInfo = OreWarActivityMgr.getOpenActivityInfo(activityData.getActivityId());
        if(!ActivityMgr.activityOverEndTime(activityInfo)){
            if (UnionRiseRankArgs.NOTIFY_TYPE_NORMAL == notifyType || UnionRiseRankArgs.NOTIFY_TYPE_JOIN == notifyType) {
                userActivityConditionData.setUnionUid(player.getUserInfo().getUnionUid());
            } else if (UnionRiseRankArgs.NOTIFY_TYPE_REMOVE == notifyType || UnionRiseRankArgs.NOTIFY_TYPE_DELETE == notifyType) {
                userActivityConditionData.setUnionUid("");
            }
        }
        OreWarActivityRankMgr.changeUnionActivityValue(activityData.getActivityId(), unionUid, player.getUserId(), userActivityConditionData, notifyType);
        player.getModule(OreWarActivityModule.class).onActivityDataChange(activityData);
    }
}
